package com.tbit.uqbike.webmanager.controller;


import com.tbit.uqbike.object.pojo.dto.AccountDayStatFinancialDTO;
import com.tbit.uqbike.object.pojo.dto.DownloadAllStatDetailDTO;
import com.tbit.manager.api.dto.OperationalDataDTO;
import com.tbit.uqbike.object.pojo.vo.*;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AccountDayStatFinancialService;
import com.tbit.uqbike.service.business.AccountDayStatService;
import com.tbit.uqbike.service.business.ArrearsLogService;
import com.tbit.uqbike.webmanager.dao.stat.AccountDayStatDao;
import com.tbit.uqbike.webmanager.util.csv.annotations.AsyncDownload;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 财务统计-财务总览
 *
 * @Author:yanlun
 * @DATE: 2022/11/1
 */
@RestController
@RequestMapping("/accountDayStatFinancialCollect")
public class AccountDayStatFinancialCollectController {
    @Autowired
    AccountDayStatFinancialService accountDayStatFinancialService;
    @Autowired
    ArrearsLogService arrearsLogService;
    @Autowired
    AccountDayStatDao accountDayStatDao;
    @Autowired
    private AccountDayStatService accountDayStatService;

    /**
     * 汇总展示
     *
     * @param operationalDataDTO
     * @return
     */
    @PostMapping("/financialSummary")
    public Result<Map<String, Object>> financialSummary(@RequestBody OperationalDataDTO operationalDataDTO) {
        Map<String, Object> map = accountDayStatService.operationalData(operationalDataDTO);
        return Result.success(map);
    }


    /**
     * 按区域统计-商户平台收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/merchantFinancial")
    public Result<AccountDayStatMerchantFinancialVo> merchantFinancial(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatMerchantFinancialVo accountDayStatMerchantFinancialVo = accountDayStatFinancialService.financeMerchantPlatform(accountDayStatFinancialDTO, false);
        return Result.success(accountDayStatMerchantFinancialVo);
    }


    /**
     * 按区域统计-运营营收收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/operateFinancial")
    public Result<AccountDayStatOperateVo> operateFinancial(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatOperateVo accountDayStatOperateVo = accountDayStatFinancialService.operateFinancial(accountDayStatFinancialDTO, false);
        return Result.success(accountDayStatOperateVo);
    }

    /**
     * 按区域统计-预存款收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/balanceFinancial")
    public Result<AccountDayStatBalanceVo> balanceFinancial(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatBalanceVo accountDayStatBalanceVo = accountDayStatFinancialService.balanceFinancial(accountDayStatFinancialDTO, false);
        return Result.success(accountDayStatBalanceVo);
    }

    /**
     * 按区域统计-用户押金收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/depositFinancial")
    public Result<AccountDayStatDepositVo> depositFinancial(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatDepositVo accountDayStatDepositVo = accountDayStatFinancialService.depositFinancial(accountDayStatFinancialDTO, false);
        return Result.success(accountDayStatDepositVo);
    }


    /**
     * 按区域统计-其他收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/otherFinancial")
    public Result<AccountDayStatOtherVo> otherFinancial(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatOtherVo accountDayStatOtherVo = accountDayStatFinancialService.otherFinancial(accountDayStatFinancialDTO, false);
        return Result.success(accountDayStatOtherVo);
    }

    /**
     * 按区域统计-欠款收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/arrearsFinancial")
    public Result<AccountDayStatArrearsVo> arrearsFinancial(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatArrearsVo accountDayStatArrearsVo = accountDayStatFinancialService.arrearsFinancial(accountDayStatFinancialDTO, false);
        return Result.success(accountDayStatArrearsVo);
    }

    /**
     * 按区域统计-礼品卡收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/giftCardFinancial")
    public Result<AccountDayStatGiftCardVO> giftCardFinancial(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatGiftCardVO dayStatGiftCardVO = accountDayStatFinancialService.giftCardFinancial(accountDayStatFinancialDTO, false);
        return Result.success(dayStatGiftCardVO);
    }

    /**
     * 按区域统计-用车虚拟金额收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/fictitiousFinancial")
    public Result<AccountDayStatFictitiousVo> fictitiousFinancial(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) throws IllegalAccessException, InstantiationException {
        AccountDayStatFictitiousVo accountDayStatFictitiousVo = accountDayStatFinancialService.fictitiousFinancial(accountDayStatFinancialDTO, false);
        return Result.success(accountDayStatFictitiousVo);
    }

    /**
     * 按天统计-总账单
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/totalFinancialByDay")
    public Result<AccountDayStatTotalVO> totalFinancialByDay(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatTotalVO accountDayStatTotalVO = accountDayStatFinancialService.totalFinancial(accountDayStatFinancialDTO);
        return Result.success(accountDayStatTotalVO);
    }

    /**
     * 按天统计-商户平台收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/merchantFinancialByDay")
    public Result<AccountDayStatMerchantFinancialVo> merchantFinancialByDay(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatMerchantFinancialVo accountDayStatMerchantFinancialVo = accountDayStatFinancialService.financeMerchantPlatform(accountDayStatFinancialDTO, true);
        return Result.success(accountDayStatMerchantFinancialVo);
    }

    /**
     * 按天统计-运营营收收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/operateFinancialByDay")
    public Result<AccountDayStatOperateVo> operateFinancialByDay(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatOperateVo accountDayStatOperateVo = accountDayStatFinancialService.operateFinancial(accountDayStatFinancialDTO, true);
        return Result.success(accountDayStatOperateVo);
    }

    /**
     * 按天统计-预存款收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/balanceFinancialByDay")
    public Result<AccountDayStatBalanceVo> balanceFinancialByDay(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatBalanceVo accountDayStatBalanceVo = accountDayStatFinancialService.balanceFinancial(accountDayStatFinancialDTO, true);
        return Result.success(accountDayStatBalanceVo);
    }

    /**
     * 按天统计-用户押金收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/depositFinancialByDay")
    public Result<AccountDayStatDepositVo> depositFinancialByDay(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatDepositVo accountDayStatDepositVo = accountDayStatFinancialService.depositFinancial(accountDayStatFinancialDTO, true);
        return Result.success(accountDayStatDepositVo);
    }

    /**
     * 按天统计-其他收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/otherFinancialByDay")
    public Result<AccountDayStatOtherVo> otherFinancialByDay(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatOtherVo accountDayStatOtherVo = accountDayStatFinancialService.otherFinancial(accountDayStatFinancialDTO, true);
        return Result.success(accountDayStatOtherVo);
    }

    /**
     * 按天统计-欠款收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/arrearsFinancialByDay")
    public Result<AccountDayStatArrearsVo> arrearsFinancialByDay(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatArrearsVo accountDayStatArrearsVo = accountDayStatFinancialService.arrearsFinancial(accountDayStatFinancialDTO, true);
        return Result.success(accountDayStatArrearsVo);
    }

    /**
     * 按天统计-礼品卡收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/giftCardFinancialByDay")
    public Result<AccountDayStatGiftCardVO> giftCardFinancialByDay(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatGiftCardVO dayStatGiftCardVO = accountDayStatFinancialService.giftCardFinancial(accountDayStatFinancialDTO, true);
        return Result.success(dayStatGiftCardVO);
    }

    /**
     * 按天统计-用车虚拟金额收支情况
     *
     * @param accountDayStatFinancialDTO
     * @return
     */
    @PostMapping("/fictitiousFinancialByDay")
    public Result<AccountDayStatFictitiousVo> fictitiousFinancialByDay(@RequestBody @Valid AccountDayStatFinancialDTO accountDayStatFinancialDTO) {
        AccountDayStatFictitiousVo accountDayStatFictitiousVo = accountDayStatFinancialService.fictitiousFinancial(accountDayStatFinancialDTO, true);
        return Result.success(accountDayStatFictitiousVo);
    }


    /**
     * 数据截止时间yyyy-MM-dd HH:mm:ss
     *
     * @param brandId
     * @param accountIds 逗号隔开
     * @return
     */
    @GetMapping("/lastDataTime")
    public Result<String> lastDataTime(Integer brandId, String accountIds) {
        Assert.notNulls("缺少必要参数", brandId);
        List<Integer> accountIdList = new ArrayList<>();
        String[] arr = accountIds.split(",");
        for (String accountId : arr) {
            accountIdList.add(Integer.parseInt(accountId));
        }
        String dateTime = accountDayStatFinancialService.lastDataTime(brandId, accountIdList);
        return Result.success(dateTime);
    }

    /**
     * 下载-by区域-商户平台营收账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按区域-商户平台营收账单")
    @PostMapping("/merchantFinancial/download")
    public Result downloadMerchant(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatMerchantFinancialDetailVO.class, false));
    }

    /**
     * 下载-by天-商户平台营收账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按天-商户平台营收账单")
    @PostMapping("/merchantFinancialByDay/download")
    public Result downloadMerchantByDay(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatMerchantFinancialDetailVO.class, true));
    }

    /**
     * 下载-by区域-运营营收收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按区域-运营营收收支账单")
    @PostMapping("/operateFinancial/download")
    public Result downloadOperate(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatOperateDetailVO.class, false));
    }

    /**
     * 下载-by天-运营营收收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按天-运营营收收支账单")
    @PostMapping("/operateFinancialByDay/download")
    public Result downloadOperateByDay(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatOperateDetailByDayVO.class, true));
    }

    /**
     * 下载-by区域-预存款收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按区域-预存款收支账单")
    @PostMapping("/balanceFinancial/download")
    public Result downloadBalance(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatBalanceDetailVO.class, false));
    }

    /**
     * 下载-by天-预存款收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按天-预存款收支账单")
    @PostMapping("/balanceFinancialByDay/download")
    public Result downloadBalanceByDay(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatBalanceDetailVO.class, true));
    }

    /**
     * 下载-by区域-押金收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按区域-押金收支账单")
    @PostMapping("/depositFinancial/download")
    public Result downloadDeposit(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatDepositDetailVO.class, false));
    }

    /**
     * 下载-by天-押金收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按天-押金收支账单")
    @PostMapping("/depositFinancialByDay/download")
    public Result downloadDepositByDay(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatDepositDetailVO.class, true));
    }

    /**
     * 下载-by区域-其他收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按区域-其他收支账单")
    @PostMapping("/otherFinancial/download")
    public Result downloadOther(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatOtherDetailVO.class, false));
    }

    /**
     * 下载-by天-其他收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按天-其他收支账单")
    @PostMapping("/otherFinancialByDay/download")
    public Result downloadOtherByDay(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatOtherDetailVO.class, true));
    }

    /**
     * 下载-by区域-礼品卡收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按区域-礼品卡收支账单")
    @PostMapping("/giftCardFinancial/download")
    public Result downloadGiftCard(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatGiftCardDetailVO.class, false));
    }

    /**
     * 下载-by天-礼品卡收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按天-礼品卡收支账单")
    @PostMapping("/giftCardFinancialByDay/download")
    public Result downloadGiftCardByDay(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatGiftCardDetailVO.class, true));
    }

    /**
     * 下载-by区域-用车虚拟收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按区域-用车虚拟收支账单")
    @PostMapping("/fictitiousFinancial/download")
    public Result downloadFictitious(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatFictitiousDetailVO.class, false));
    }

    /**
     * 下载-by天-用车虚拟收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按天-用车虚拟收支账单")
    @PostMapping("/fictitiousFinancialByDay/download")
    public Result downloadFictitiousByDay(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatFictitiousDetailVO.class, true));
    }

    /**
     * 下载-by区域-欠款收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按区域-欠款收支账单")
    @PostMapping("/arrearsFinancial/download")
    public Result downloadArrears(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatArrearsDetailVO.class, false));
    }

    /**
     * 下载-by天-欠款收支账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按天-欠款收支账单")
    @PostMapping("/arrearsFinancialByDay/download")
    public Result downloadArrearsByDay(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.download(downloadDTO, AccountDayStatArrearsDetailVO.class, true));
    }

    /**
     * 下载-by天-总账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "按天-总账单")
    @PostMapping("/totalFinancial/download")
    public Result downloadTotalFinancial(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.downloadTotalFinancial(downloadDTO));
    }

    /**
     * 下载-汇总导出
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "汇总账单")
    @PostMapping("/financialSummary/download")
    public Result financialSummaryDownload(@RequestBody @Valid AccountDayStatFinancialDTO downloadDTO) {
        return Result.success(accountDayStatFinancialService.financialSummaryDownload(downloadDTO));
    }

    /**
     * 总计表下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "总计表")
    @PostMapping("/downloadAllStatDetail")
    public Result downloadAllStatDetail(@RequestBody @Valid DownloadAllStatDetailDTO downloadDTO) {
        String url = accountDayStatFinancialService.downloadAllStatDetail(downloadDTO);
        return Result.success(url);
    }

}
